<template>
  <div class="app-container">
    <el-tree
      :data="treeData"
      :props="treeProps"
      default-expand-all
      :expand-on-click-node="false"
      highlight-current
      style="display:inline-block">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span style="margin-left: 60px">
          <el-button v-if="node.level!==3" type="text" size="mini" @click="() => appendType(node,data)">增加</el-button>
          <el-button v-if="node.level!==1" type="text" size="mini" @click="() => editType(node, data)">修改</el-button>
          <el-button v-if="node.level!==1" type="text" size="mini" @click="handleDelete(data)">删除</el-button>
        </span>
      </span>
    </el-tree>
    <!-- 添加或修改材料类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类型名称" prop="materiatypeName">
          <el-input v-model="form.materiatypeName" placeholder="请输入类型名称" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <span v-text="form.level"></span>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterialType, getMaterialType, delMaterialType, addMaterialType, updateMaterialType,getTreeData } from "@/api/material/materialType";

export default {
  name: "MaterialType",
  data() {
    return {
      treeData:[],
      treeProps: {
        label: 'materiatypeName',
        children: 'children'
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料类型表格数据
      materialTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materiatypeName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materiatypeName: [
          { required: true, message: "类型名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    getTypeTree(){
      getTreeData().then(res=>{
        this.treeData = [res.data];
      })
    },
    /** 查询材料类型列表 */
    getList() {
      this.loading = true;
      listMaterialType(this.queryParams).then(response => {
        this.materialTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getTypeTree();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        materiatypeId: null,
        materiatypeName: null,
        materiatypePid: null,
        level: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加材料类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const materiatypeId = row.materiatypeId || this.ids
      getMaterialType(materiatypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改材料类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materiatypeId != null) {
            updateMaterialType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaterialType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materiatypeIds = row.materiatypeId || this.ids;
      this.$modal.confirm('是否确认删除材料类型编号为"' + materiatypeIds + '"的数据项？').then(function() {
        return delMaterialType(materiatypeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    appendType(data, node){
      this.reset();
      this.title="添加材料类型";
      this.open=true;
      this.form.level=node.level+1;
      this.form.materiatypePid=node.materiatypeId;
    },
    editType(data, node){
      this.title="修改材料类型";
      this.open=true;
      getMaterialType(node.materiatypeId).then(response=>{
        this.form=response.data;
      })
        .catch(()=>{});
    },
  }
};
</script>
<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  justify-content: space-between;
  font-size: 15px;
  padding-right: 10px;
}
</style>
